package servlets.site;

import beans.CategoryFacade;
import beans.VideoGameFacade;
import helpers.ServletHelper;
import helpers.XMLHelper;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

@WebServlet(name = "XMLServlet", urlPatterns = {"/XMLServlet"})
public class XMLServlet extends HttpServlet {

    ///////////////////////////////GESTIONNAIRES///////////////////////////////
    @EJB
    private VideoGameFacade jeuVideoFacade;
    @EJB
    private CategoryFacade categorieFacade;
    ///////////////////////////////////////////////////////////////////////////

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        String view = request.getParameter("view");

        if (view != null) {
            if (view.equals("keywords")) {
                try {
                    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
                    Element eKeywords = document.createElement("keywords");
                    XMLHelper.printKeywords(document, eKeywords, jeuVideoFacade.getTitles(), "Video Game Title");
                    XMLHelper.printKeywords(document, eKeywords, jeuVideoFacade.getPublishers(), "Publisher Name");
                    XMLHelper.printKeywords(document, eKeywords, categorieFacade.getNames(), "Category Name");
                    document.appendChild(eKeywords);
                    out.println("<?xml version=\"1.0\" ?>");
                    XMLHelper.printDOMTree(out, document);
                } catch (ParserConfigurationException ex) {
                    Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
                }
            } /**else if (view.startsWith("tags")) {
                String[] params = view.split("_");
                int id = 0;

                if (params != null && params.length == 2) {
                    id = ServletHelper.getIntegerValue(params[1]);
                }

                if (id > 0) {
                    Collection<Tag> tags = tagFacade.getByVideoGameID(id);
                    XMLHelper.printTags(out, tags, jeuVideoFacade);
                } else {
                    Collection<Tag> tags = tagFacade.getAll();
                    XMLHelper.printTags(out, tags, jeuVideoFacade);
                }
            }*/
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
